/**
 * 
 */
package org.hackystat.ui.sensordatabrowser.client;

import java.util.ArrayList;
import java.util.Iterator;

import org.hackystat.ui.sensordatabrowser.client.common.Event;


import com.google.gwt.user.client.ui.Tree;

/**
 * This class embodies an ordered list of SDBEvents.
 * @author David J. Nickles
 *
 */
public class EventTree extends Tree {
  
  private static int oddoreven;
  private int count;
  private Session session;

  /**
   * Creates an empty SDVEventColelction for classifying and storing SDVEvents.
   * @param session information.
   */
  public EventTree(Session session) {
    this.session = session;
  }

  /**
   * Initializes a SDBEventCollection for classifying and storing SDBEvents.
   * @param list of the request response from the servlet
   * 
   * @gwt.typeArgs list <java.util.ArrayList<java.lang.String>>
   */
  private final void populate(ArrayList list) {
    /** @gwt.typeArgs list <java.util.ArrayList<java.lang.String>> */
    Iterator it = list.listIterator();
    while (it.hasNext()) {
      Event event = (Event)it.next();
      EventTreeItem e = new EventTreeItem(event, ++oddoreven, this.session);
      this.count += event.getCount();
      addItem(e);
    }
  }
  
  /**
   * This method resets the Tree to contain only the new list of items.
   * @param list of items to put into the Tree.
   * @return a Tree reset to contain only the new list of items.
   * 
   * @gwt.typeArgs list <java.lang.String>
   */
  public Tree resetTree(ArrayList list) {
    this.count = 0;
    clear();
    populate(list);
    return this;
  }
  
  /**
   * Gets the count of all data items in all the Events in this tree.
   * @return the count of new elements.
   */
  public int getDataCount() {
    return this.count;
  }
}